//历史消息的api操作方法
import store from '@/store';
import tim from '@/utils/tim.js';

/**
 * 触发vuex中historyUpdateFlag值的改变
 * @param flag
 * @returns {Promise<void>}
 */
export async function historyUpdateFlag(flag = false) {
	//刷新消息之前 看当前是否有一个会话，如果有把这个消息都置为已读
	await flushSetUnread();
	store.commit('imStore/RESET_HISTORY_UPDATE_FLAG', flag);
}

/**
 * 从当前会话中，把当前会话中的消息置为已读
 */
export async function flushSetUnread() {
	let { type = '', groupId = '', userId = '' } = JSON.parse(
		store.state.imStore.currentSessionContent || '{}'
	);

	console.log(
		'flushSetUnreadflushSetUnread',
		type,
		groupId,
		store.state.imStore.currentSessionContent
	);

	if (type) {
		if (type === 'friend') {
			//好友
			await tim.setMessageRead({
				conversationID: `C2C${userId}`,
			});
		} else if (type === 'group') {
			//群组
			await tim.setMessageRead({ conversationID: `GROUP${groupId}` });
		}
	}
}
